package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {


    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;
    /**
     * 关注和取消关注操作
     */
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        //1.获取当前登录用户
        Long userID = UserHolder.getUser().getId();
        String key = "follows:" + userID;
        //2.判断是关注还是取消关注操作
        if(isFollow) {
            //3.关注操作,存入数据库  并且也存入 redis 的 set 集合中
            Follow follow = new Follow();
            follow.setUserId(userID);
            follow.setFollowUserId(followUserId);
            boolean isSuccess = save(follow);
            if(isSuccess) {
                //存入当前登录用户关注的人 张三(rose,mike,......)
                stringRedisTemplate.opsForSet().add(key,followUserId.toString());
            }

        } else {
            //4.取消关注操作,从数据库中移除
            //delete  from tb_follow where user_id = ? and follow_user_id = ?
            boolean isSuccess = remove(new QueryWrapper<Follow>().eq("user_id", userID)
                    .eq("follow_user_id", followUserId));
            if(isSuccess) {
                stringRedisTemplate.opsForSet().remove(key,followUserId.toString());
            }

        }
        return Result.ok();
    }


    /**
     * 判断当前用户是否关注了当前博客用户
     * @param followUserId
     * @return
     */
    @Override
    public Result isFollow(Long followUserId) {
        //查询当前登录的用户是否关注了博客用户
        Long userId = UserHolder.getUser().getId();
        Long count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        return Result.ok(count > 0);
    }

    /**
     * 判断当前登录的用户和当前笔记用户 的共同关注列表
     * @param id
     * @return
     */
    @Override
    public Result followCommons(Long id) {
        //1.求当前登录用户
        Long userId = UserHolder.getUser().getId();
        String key1 = "follows:" + userId;

        //2.当前笔记的用户
        String key2 = "follows:" + id;

        //3.求出 key1 和 key2 的交集
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        //4.把当前的集合转化为userDto对象
        if(intersect == null || intersect.isEmpty()) {
            return Result.fail("你们暂时无共同关注哦！");
        }
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> userDTOS = userService.listByIds(ids)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(userDTOS);
    }
}
